package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author anshuai
 * @create 2021-02-02 16:17
 */
@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private CommentsService commentsService;

    /**
     * 视频-发布视频
     * POST /smallVideos
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        return videoService.saveVideos(videoThumbnail, videoFile);
    }

    /**
     * 视频-小视频列表
     * GET /smallVideos
     */
    @GetMapping
    public ResponseEntity findVideoList(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize) {
        if (page <= 0) {
            page = 1;
        }

        PageResult<VideoVO> pageResult = videoService.findVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频-关注用户
     * POST /smallVideos/:uid/userFocus
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity userFocus(@PathVariable("id") Long targetUserId){
        return videoService.userFocus(targetUserId);
    }

    /**
     * 视频-取消关注
     * POST /smallVideos/:uid/userUnFocus
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("id") Long targetUserId){
        return videoService.userUnFocus(targetUserId);
    }

    /**
     * 视频-点赞
     * POST /smallVideos/:id/like
     */
    @PostMapping("/{id}/like")
    public ResponseEntity likeVideo(@PathVariable("id") String videoId){
        return videoService.likeVideo(videoId);
    }

    /**
     * 视频-取消点赞
     * POST /smallVideos/:id/dislike
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity disLikeVideo(@PathVariable("id") String videoId){
        return videoService.disLikeVideo(videoId);
    }

    /**
     * 视频-评论列表
     * GET /smallVideos/:id/comments
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findComments(@PathVariable("id") String videoId,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize){
        return videoService.findComments(videoId,page,pagesize);
    }

    /**
     * 视频-评论发布
     * POST /smallVideos/:id/comments
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") String videoId,
                                       @RequestBody Map map){
        String content = (String) map.get("comment");
        return videoService.saveComments(videoId,content);
    }

    /**
     * 视频-评论点赞
     * POST /smallVideos/comments/:id/like
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") String commentId){
        return commentsService.likeComment(commentId);
    }

    /**
     * 视频-评论取消点赞
     * POST /smallVideos/comments/:id/dislike
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity disLikeComment(@PathVariable("id") String commentId){
        return commentsService.disLikeComment(commentId);
    }
}
